package com.wechat.service;

import com.wechat.dao.QuestionDao;
import com.wechat.dao.QuestionDetailDao;
import com.wechat.dao.QuestionLibDao;
import com.wechat.dao.extend.QuestionDetailExtDao;
import com.wechat.entity.Question;
import com.wechat.entity.QuestionDetail;
import com.wechat.entity.QuestionLib;
import com.wechat.entity.extend.QuestionDetailExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by ycx on 2018/4/11.
 */
@Service
public class QuestionService {

    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private QuestionLibDao questionLibDao;
    @Autowired
    private QuestionDetailDao questionDetailDao;
    @Autowired
    private QuestionDetailExtDao questionDetailExtDao;

    @Transactional(rollbackFor = Exception.class)
    public int saveQuestion(Question record) {
        int result = questionDao.save(record);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateQuestion(Question record) {
        int result = questionDao.update(record);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public int saveQuestionDetail(QuestionDetail record) {
        //如果已经加过同一个题目则不增加
        List<Integer> resultList = questionDetailDao.selIdsByQuestionId(record.getQuestionId());
        if(resultList.contains(record.getQuestionLibId())){
            return 0;
        }
        int result = questionDetailDao.save(record);
        return result;
    }

    public Question findQuestionByRechargeId(Long rechargeId) {
        return questionDao.findByRechargeId(rechargeId);
    }

    public Question findQuestion(Integer id) {
        Question result = questionDao.findById(id);
        return result;
    }



    public QuestionDetail findQuestionDetail(Integer id) {
        return questionDetailDao.findById(id);
    }

    public int questionNum(Integer questionId) {
        int result = questionDetailDao.questionNum(questionId);
        return result;
    }

    /**
     * 根据传递的参数查询出题记录明细表
     *
     * @return
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public List<QuestionDetailExt> questionDetailExtList(Integer questionId){
        return questionDetailExtDao.list(questionId);
    }

    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public List<Question> questionJoinRechargeList(Integer userId,
                                                   Integer tradeStatus,
                                                   Integer amountStatus,
                                                   Date rechargeBegin,
                                                   Date rechargeEnd){
        return questionDao.questionJoinRechargeList(userId,tradeStatus,amountStatus,rechargeBegin,rechargeEnd);
    }

    /**
     * 查询与之前不重复的题目
     *
     * @param questionId
     * @return
     */
    public QuestionLib findNotRepeatQuestion(Integer questionId) {

        List<Integer> resultList = questionDetailDao.selIdsByQuestionId(questionId);

        if(resultList.size() == 0){
            resultList = null;
        }

        List<Integer> idList = questionLibDao.selRemainUndeletedIds(resultList);

        int num = new Random().nextInt(idList.size());

        Integer id = idList.get(num);
        QuestionLib result = questionLibDao.findById(id);

        return result;
    }

}
